﻿using System;
using System.Diagnostics;
using System.IO;

namespace CDude.Vita
{
    internal class AppXml
    {
        private readonly string pythonpath = Path.Combine(Environment.GetEnvironmentVariable("SCE_PSM_SDK"), "tools\\Python\\python.exe");
        public string project_name;
        public string managed_heap_size = "32768";
        public string resource_heap_size = "65536";
        public string feature_list = "";

        public AppXml(string project_name)
        {
            this.project_name = project_name;
        }

        private string XML
        {
            get
            {
                return "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<application project_name=\"" + project_name + "\" version=\"1.00\" default_locale=\"en-US\">\n" +
                "	<runtime_config>\n" +
                "		<memory managed_heap_size=\"" + managed_heap_size + "\" resource_heap_size=\"" + resource_heap_size + "\" />\n" +
                "	</runtime_config>\n" +
                "	<feature_list>\n" +
                feature_list +
                "	</feature_list>\n" +
                "</application>\n";
            }
        }

        public void SaveInfo(string outputfolder)
        {
            using (StreamWriter appxmlwriter = new StreamWriter(Path.Combine(outputfolder, "app.xml"), false))
                appxmlwriter.Write(XML);

            string pythonargs = "\"" + Path.Combine(Environment.GetEnvironmentVariable("SCE_PSM_SDK"), "tools\\PublishingUtility\\cxml\\appinfo\\appinfo_compiler.py") + "\" -o \"" + Path.Combine(outputfolder, "app.info") + "\" \"" + Path.Combine(outputfolder, "app.xml") + "\"";

            Process appinfocompiler = Process.Start(pythonpath, pythonargs);
            appinfocompiler.WaitForExit();

            File.Delete(Path.Combine(outputfolder, "app.xml"));
        }
    }
}